/***************************************************************************
*  File name: svrInit.cpp
*   Describe: 数据库连接初始化
* Created on: 2016-8-6
*     Author: dengxiudong
****************************************************************************/
#include "SysInit.h"

#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>

#include <acl_cpp/acl_cpp_init.hpp>
#include <acl_cpp/redis/redis.hpp>
#include <acl_cpp/redis/redis_client.hpp>
#include <acl_cpp/redis/redis_client_cluster.hpp>

#include <loong/IniFile.h>
#include <loong/Logger.h>
#include <loong/MySqlDB.h>


#include "SdbOpt.h"

loong::MySqlPool *g_sqlPool = nullptr;
acl::redis *g_redisCmd=nullptr;
acl::redis_client *g_redisCli=nullptr;
acl::redis_client_cluster *g_cluster=nullptr;
SdbOpt *g_sdbop=nullptr;

bool readConfig(Config &config)
{
    loong::IniFile cfg;
    if (!cfg.init("./dtcsvr.ini"))
    {
        std::cout<<"Read config file error!"<<std::endl;
    }

    //-------------------------------mysql-----------------------------------
    if (!cfg.readString("mysql", "ip", config.mysqlIP))
    {
        ERROR("read mysql ip failed!");
        return false;
    }
    INFO("mysql ip: %s", config.mysqlIP.c_str());

    if (!cfg.readInt("mysql", "port", config.mysqlPort))
    {
        ERROR("read mysql port failed!");
        return false;
    }
    INFO("mysql port: %d", config.mysqlPort);
	
	if (!cfg.readInt("mysql", "poolsize", config.mysqlPoolSize))
	{
		ERROR("read mysql pool size failed!");
		return false;
	}
	INFO("mysql pool size: %d", config.mysqlPoolSize);

    if (!cfg.readString("mysql", "username", config.mysqlUserName))
    {
        ERROR("read mysql username failed!");
        return false;
    }
    INFO("mysql ip: %s", config.mysqlUserName.c_str());

    if (!cfg.readString("mysql", "passwd", config.mysqlPasswd))
    {
        ERROR("read mysql passwd failed!");
        return false;
    }
    INFO("mysql passwd: %s", config.mysqlPasswd.c_str());

    if (!cfg.readString("mysql", "dbname", config.mysqlDB))
    {
        ERROR("read mysql dbname failed!");
        return false;
    }
    INFO("mysql dbname: %s", config.mysqlDB.c_str());

    //-------------------------------redis-------------------------------------
    if (!cfg.readString("redis", "addr", config.redisAddr))
    {
        ERROR("read redis addr failed!");
        return false;
    }
    INFO("redis addr: %s", config.redisAddr.c_str());

    if (!cfg.readString("redis", "passwd", config.redisPasswd))
    {
        ERROR("read redis passwd failed!");
        return false;
    }
    INFO("redis passwd: %s", config.redisPasswd.c_str());

    if (!cfg.readInt("redis", "iscluster", config.redisCluster))
    {
        ERROR("read redis cluster module failed!");
        return false;
    }
    INFO("redis iscluster: %d", config.redisCluster);

    //-----------------------------SequoidDB---------------------------------
    if (!cfg.readString("sdb", "ip", config.sdbIP))
    {
        ERROR("read sdb ip failed!");
        return false;
    }
    INFO("sdb ip: %s", config.sdbIP.c_str());

    if (!cfg.readInt("sdb", "port", config.sdbPort))
    {
        ERROR("read sdb port failed!");
        return false;
    }
    INFO("sdb port: %d", config.sdbPort);

    if (!cfg.readString("sdb", "username", config.sdbUserName))
    {
        ERROR("read sdb username failed!");
        return false;
    }
    INFO("mysql ip: %s", config.sdbUserName.c_str());

    if (!cfg.readString("sdb", "passwd", config.sdbPasswd))
    {
        ERROR("read sdb passwd failed!");
        return false;
    }
    INFO("sdb passwd: %s", config.sdbPasswd.c_str());

    return true;
}

int sysInit()
{
    Config cfg;
    //读取配置文件
    readConfig(cfg);

    //连接mysql数据库
    try
    {
        g_sqlPool=new loong::MySqlPool(cfg.mysqlPoolSize);
        g_sqlPool->create(cfg.mysqlIP, cfg.mysqlPort, cfg.mysqlUserName, cfg.mysqlPasswd, cfg.mysqlDB);
    }
    catch(loong::DBException &e)
    {
        ERROR("mysql error: %s", e.getMsg().c_str());
    }
    catch(exception &e)
    {
        ERROR("create mysqlpool object failed: %s", e.what());
    }

    INFO("init mysqldb connetct pool success;");

    //初始化redis集群连接
    int connTimeout = 30;
    int rwTimeout = 30;

    acl::acl_cpp_init();
    if (cfg.redisCluster==1)
    {
        g_cluster=new acl::redis_client_cluster();
        g_cluster->init(nullptr, cfg.redisAddr.c_str(), 100, connTimeout, rwTimeout);
        g_cluster->set_password("default",cfg.redisPasswd.c_str());
        g_redisCmd=new acl::redis(g_cluster);
    }
    else
    {
        g_redisCli=new acl::redis_client(cfg.redisAddr.c_str(), connTimeout, rwTimeout);
        g_redisCli->set_password(cfg.redisPasswd.c_str());
        g_redisCmd=new acl::redis(g_redisCli);
    }
    INFO("init redis success;");

    //初始化SequoiaDB
    g_sdbop = new SdbOpt();
    if (!g_sdbop->initSdb(cfg.sdbIP, cfg.sdbPort, cfg.sdbUserName, cfg.sdbPasswd))
    {
        ERROR("Init sdb failed!");
        return -1;
    }
    INFO("init sdb success;");
	
    return 0;
}
